home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech's Sprocket™ / SprocketGX / AppSpecific / GXDocFile.cp < prev    next >
Encoding:
Text File  |  1994-10-17  |  5.9 KB  |  251 lines  |  [TEXT/MMCC]

  1. /*
  2.     File:        GXDocFile.cp
  3.  
  4.     Contains:    The prototype GX doc file
  5.                 
  6.     Written by: Jon Summers
  7.     
  8.     Copyright:    © 1994 by Jon Summers, all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.     
  12.  */
  13.  
  14. #include "GXDocFile.h"
  15. #include "AppLib.h"
  16.  
  17. #ifndef    __QDGXHeaders__
  18.     #include    "QDGXHeaders.h"
  19. #endif
  20.  
  21. TGXDocFile::TGXDocFile()
  22.  : fPagesInDoc(0)
  23. {
  24. }
  25.  
  26. TGXDocFile::~TGXDocFile()
  27. {
  28. }
  29.  
  30. #if  defined (powerpc) || defined (__powerpc)
  31.     #pragma  options    align=mac68k
  32. #endif
  33.  
  34. typedef struct    // PrintFileFormat
  35. {
  36.     long version;
  37.     long    offsetPageDir;
  38. } PrintFileFormat, *PrintFileFormatPtr;
  39.  
  40. typedef struct    // PageDirInfo
  41. {
  42.     long    size;
  43.     long    offset;
  44. } PageDirInfo, *PageDirInfoPtr;
  45.  
  46. typedef struct    // PageDirectory
  47. {
  48.     long         numPages;
  49.     PageDirInfo        info[1];
  50. } PageDirectory, *PageDirPtr;
  51.  
  52. typedef struct    // PrintFileSpool
  53. {
  54.     gxSpoolBlock        spool;
  55.     FSSpec                spec;
  56.     OSErr                err;
  57.     short                fileRef;
  58.     long                filePosn;
  59. } PrintFileSpool, *PrintFileSpoolPtr;
  60.  
  61. #if  defined (powerpc) || defined (__powerpc)
  62.     #pragma  options    align=reset
  63. #endif
  64.  
  65. #ifdef __cplusplus
  66. extern "C" {
  67. #endif
  68.  
  69. #ifndef        KnowsStatic        // Object Master
  70. static  
  71. #endif
  72. long PrintFileSpoolProc(gxSpoolCommand aSpoolCmd, PrintFileSpoolPtr pBlock)
  73. {
  74.     OSErr    anErr = noErr;
  75.     long        aIoCount = pBlock->spool.count;
  76.  
  77.    switch (aSpoolCmd)
  78.    {
  79.       case gxOpenReadSpool:
  80.         anErr = FSpOpenDF(&pBlock->spec, fsRdPerm, &pBlock->fileRef);
  81.         pBlock->err = anErr;
  82.           break;
  83.       case gxOpenWriteSpool:
  84.         anErr = FSpOpenDF(&pBlock->spec, fsWrPerm, &pBlock->fileRef);
  85.         pBlock->err = anErr;
  86.           break;
  87.       
  88.       case gxReadSpool:
  89.         if (pBlock->err)
  90.             anErr = pBlock->err;
  91.         else
  92.         {
  93.             anErr = SetFPos(pBlock->fileRef, fsFromStart, pBlock->filePosn);
  94.             if ( ! anErr)
  95.             {
  96.                 anErr = FSRead(pBlock->fileRef, &aIoCount, pBlock->spool.buffer);
  97.                 pBlock->err = anErr;
  98.                 if (anErr == eofErr)
  99.                     anErr = noErr;
  100.             }
  101.             if ( ! anErr)
  102.                 pBlock->filePosn += aIoCount;
  103.             else
  104.                 pBlock->err = anErr;
  105.         }
  106.         break;
  107.  
  108.       case gxWriteSpool:
  109.         if (pBlock->err)
  110.             anErr = pBlock->err;
  111.         else
  112.         {
  113.             anErr = SetFPos(pBlock->fileRef, fsFromStart, pBlock->filePosn);
  114.             if ( ! anErr)
  115.                 anErr = FSWrite(pBlock->fileRef, &aIoCount, pBlock->spool.buffer);
  116.             pBlock->err = anErr;
  117.             if ( ! anErr || (anErr == dskFulErr))
  118.                 pBlock->filePosn += aIoCount;
  119.         }
  120.         break;
  121.       
  122.       case gxCloseSpool:
  123.             if (pBlock->fileRef)
  124.                 anErr = FSClose(pBlock->fileRef);
  125.             pBlock->fileRef = 0;
  126.       break;
  127.    }
  128.    return anErr;
  129. }
  130.  
  131. #ifdef __cplusplus
  132. }
  133. #endif
  134.  
  135. void InitializePrintFileSpool(PrintFileSpoolPtr pBlock);
  136. void InitializePrintFileSpool(PrintFileSpoolPtr pBlock)
  137. {
  138. #ifndef        powerpc
  139.     pBlock->spool.spoolProcedure = (gxSpoolProcPtr)PrintFileSpoolProc;
  140. #else
  141.     pBlock->spool.spoolProcedure = NewgxSpoolProcPtr(PrintFileSpoolProc);
  142. #endif
  143.     pBlock->err = noErr;
  144.     pBlock->fileRef = 0;
  145.     pBlock->spool.buffer = nil;
  146.     pBlock->spool.bufferSize = 0;
  147. }
  148.  
  149. #ifndef  powerc
  150. #define CleanupPrintFileSpool(p)
  151. #else
  152. void CleanupPrintFileSpool(PrintFileSpoolPtr pBlock);
  153. void CleanupPrintFileSpool(PrintFileSpoolPtr pBlock)
  154. {
  155.        DisposeRoutineDescriptor(pBlock->spool.spoolProcedure);
  156. }
  157. #endif
  158.  
  159. OSErr    TGXDocFile::GetDocNumOfPages(void)
  160. {
  161.     PrintFileSpool        aPrFileSpool;
  162.     PrintFileFormat        aPrFileFormat;
  163.     PageDirectory        aPageDirectory;    
  164.     
  165. // Ref: IM-QD GX: ENv pp7-51,52
  166. // open data fork
  167.     InitializePrintFileSpool(&aPrFileSpool);
  168.     GetFSSpec(&aPrFileSpool.spec);
  169.     fErr = PrintFileSpoolProc(gxOpenReadSpool, &aPrFileSpool);
  170.     if (fErr)        return fErr;
  171.     
  172. // read PrintFile Format to get offset to page directory
  173.     aPrFileSpool.spool.buffer = &aPrFileFormat;
  174.     aPrFileSpool.spool.count = sizeof(PrintFileFormat);
  175.     aPrFileSpool.filePosn = 0;
  176.     fErr = PrintFileSpoolProc(gxReadSpool, &aPrFileSpool);
  177.     if (fErr)        goto  Exit_CloseFile;
  178.  
  179. // read the page directory
  180.     aPrFileSpool.spool.buffer = &aPageDirectory;
  181.     aPrFileSpool.spool.count = sizeof(PageDirectory);
  182.     aPrFileSpool.filePosn = aPrFileFormat.offsetPageDir;
  183.     fErr = PrintFileSpoolProc(gxReadSpool, &aPrFileSpool);    // if pagenum is too great we may get eofErr
  184.     if (fErr)        goto  Exit_CloseFile;
  185.     fPagesInDoc = aPageDirectory.numPages;
  186.     
  187. /**** Cleanup ****/
  188. Exit_CloseFile:
  189.     (void)PrintFileSpoolProc(gxCloseSpool, &aPrFileSpool);
  190. Exit_Cleanup:
  191.     CleanupPrintFileSpool(&aPrFileSpool);
  192.     return fErr;
  193. }
  194.  
  195. OSErr    TGXDocFile::ReadPageShape(long  thePageNum, gxShape* pShape, gxViewPort  theViewPort)
  196. {
  197.     PrintFileSpool        aPrFileSpool;
  198.     PrintFileFormat        aPrFileFormat;
  199.     PageDirInfo            aPgDirInfo;
  200.     
  201.     fErr = GetDocNumOfPages();
  202.     if (fErr)
  203.         return fErr;
  204.  
  205. // enough pages in directory?
  206.     if (thePageNum > DocNumOfPages())
  207.     {
  208.         fErr = eofErr;
  209.         return fErr;
  210.     }
  211. // Ref: IM-QD GX: ENv pp7-51,52
  212. // open data fork
  213.     InitializePrintFileSpool(&aPrFileSpool);
  214.     GetFSSpec(&aPrFileSpool.spec);
  215.     fErr = PrintFileSpoolProc(gxOpenReadSpool, &aPrFileSpool);
  216.     if (fErr)        return fErr;
  217.     
  218. // read PrintFile Format to get offset to page directory
  219.     aPrFileSpool.spool.buffer = &aPrFileFormat;
  220.     aPrFileSpool.spool.count = sizeof(PrintFileFormat);
  221.     aPrFileSpool.filePosn = 0;
  222.     fErr = PrintFileSpoolProc(gxReadSpool, &aPrFileSpool);
  223.     if (fErr)        goto  Exit_CloseFile;
  224.  
  225. // read the page directory
  226.     aPrFileSpool.spool.buffer = &aPgDirInfo;
  227.     aPrFileSpool.spool.count = sizeof(PageDirInfo);
  228.     aPrFileSpool.filePosn = aPrFileFormat.offsetPageDir;
  229.     aPrFileSpool.filePosn += sizeof(long);
  230.     aPrFileSpool.filePosn += (thePageNum-1)*sizeof(PageDirInfo);
  231.     fErr = PrintFileSpoolProc(gxReadSpool, &aPrFileSpool);    // if pagenum is too great we may get eofErr
  232.     if (fErr)        goto  Exit_CloseFile;
  233.     
  234. //close the file: we have offset of flat shape in aPgDirInfo.offset and its flat size in aPgDirInfo.size
  235.     fErr = PrintFileSpoolProc(gxCloseSpool, &aPrFileSpool);
  236.     if (fErr)        goto  Exit_Cleanup;
  237.     
  238.     InitializePrintFileSpool(&aPrFileSpool);
  239.     aPrFileSpool.filePosn = aPgDirInfo.offset;
  240.     *pShape = GXUnflattenShape(&aPrFileSpool.spool, 1, &theViewPort);
  241.     if (*pShape == nil)
  242.         fErr = GXGetGraphicsError(nil);
  243.     goto  Exit_Cleanup;
  244.  
  245. /**** Cleanup ****/
  246. Exit_CloseFile:
  247.     (void)PrintFileSpoolProc(gxCloseSpool, &aPrFileSpool);
  248. Exit_Cleanup:
  249.     CleanupPrintFileSpool(&aPrFileSpool);
  250.     return fErr;
  251. }